function plusReady() {
    // 是否显示欢迎页
    var _displaywelcome = true, _wgtVersion, _appVersion, _mainPage;
    var welcomePage = function () {
        this.init();
        this.bindEvent();
    };

    welcomePage.prototype = {
        // 初始化
        init: function () {
            var model = this;
            // 检查升级包版本
            this.checkVersion();

            // 获取本地应用资源版本号
            plus.runtime.getProperty(plus.runtime.appid, function (inf) {
                var version = getLocalStorage(STORAGE_MANAGER.version);
                if (inf.version != version) {
                    // 欢迎页
                    _appVersion = inf.version;
                    $(".banner").show();
                    $(".slider").touchSlider({
                        width: $(window).width(),
                        height: $(window).height(),
                        during: 3000,
                        autoplay: false,
                        showindr: true,
                        move: function (currindex, total) {
                            if ((currindex + 1) == total) {
                                $(".focus").hide();
                                setTimeout(function () {
                                    $("#btnenter").show();
                                }, 300);
                            } else {
                                $("#btnenter").hide();
                                $(".focus").show();
                            }
                        }
                    });
                } else {
                    // 广告倒计时
                    $("#advdiv").show();
                    var time = 5;
                    var timerwatch = setInterval(function () {
                        time = time - 1;
                        if (time == 0) {
                            window.clearInterval(timerwatch);
                            model.enterApp();
                            return;
                        }
                        $("#timerwatch").text(time + "s");
                    }, 1000);
                }
                plus.navigator.closeSplashscreen();
            });

            _mainPage = plus.webview.create("../home/main.html", "../home/main.html", {
                scrollIndicator: 'none',
                scalable: false
            });


        },

        // 绑定事件
        bindEvent: function () {
            var model = this;
            // 欢迎页进入APP
            $("#btnenter").on("tap", function () {
                setLocalStorage(STORAGE_MANAGER.version, _appVersion);
                model.enterApp();
            });

            // 广告进入APP
            $("#btnadventer").on("tap", function () {
                model.enterApp();
            });

        },

        // 进入APP
        enterApp: function () {
            plus.navigator.setFullscreen(false);         
            _mainPage.show();
        },

        // 检查更新
        checkVersion: function () {
            var model = this;
            var networkType = plus.networkinfo.getCurrentType();
            // 仅无线网下更新
            if (networkType != plus.networkinfo.CONNECTION_WIFI) {
                return;
            }
            $.ajax({
                type: 'Get',
                async: false,
                url: 'http://www.o2abc.com/api/version/index'
            }).done(function (data) {
                if (data.state != "success") {
                    console.log(data.message);
                    return;
                }
                var versionInfo = data.data;
                var wgtversion = getLocalStorage(STORAGE_MANAGER.wgtversion);
                if (!wgtversion) {
                    // TODO 为了发布APPSORE，防止检查出下载
                    setLocalStorage(STORAGE_MANAGER.wgtversion, versionInfo.js_version);
                    return;
                }

                // 升级APP
                if (versionInfo.js_version != wgtversion) {
                    _wgtVersion = versionInfo.js_version;
                    model.downloadWgt();
                }
            }).fail(function () {
                model.enterApp();
            });
        },

        // 下载升级包wgt文件
        downloadWgt: function () {
            var model = this;
            var wgtUrl = "http://www.o2abc.com/upload/soolife.wgt";
            plus.nativeUI.showWaiting("  下载中...  ");
            plus.downloader.createDownload(wgtUrl, {filename: "_doc/update/"}, function (d, status) {
                if (status == 200) {
                    console.log("下载wgt成功：" + d.filename);
                    model.installWgt(d.filename);
                } else {
                    console.log("下载wgt失败！");
                    plus.nativeUI.closeWaiting();
                    plus.nativeUI.alert("下载失败！");
                }

            }).start();
        },

        // 更新应用资源
        installWgt: function (path) {
            plus.nativeUI.showWaiting("  更新中...  ");
            plus.runtime.install(path, {}, function () {
                // 更新版本
                setLocalStorage(STORAGE_MANAGER.wgtversion, _wgtVersion);
                plus.nativeUI.closeWaiting();
                console.log("安装wgt文件成功！");
                plus.nativeUI.toast("更新完成！");
                plus.runtime.restart();
            }, function (e) {
                plus.nativeUI.closeWaiting();
                console.log("安装wgt文件失败[" + e.code + "]：" + e.message);
                plus.nativeUI.alert("更新失败[" + e.code + "]：" + e.message);
            });
        }
    };

    var _welcomePage = new welcomePage();
}

if (window.plus) {
    plusReady();
} else {
    document.addEventListener('plusready', plusReady, false);
}
